package com.yingli.network;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

//import org.apache.http.client.HttpClient;

public class OkHttpActivity extends AppCompatActivity {

    private TextView tvWeb;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_httpclient);
//       requestData();
//        HttpClient
    }

    private void requestData() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                OkHttpClient httpClient = null;
                Response response = null;
                httpClient = new OkHttpClient().newBuilder().connectTimeout(3500, TimeUnit.MILLISECONDS).build();
                Request request = new Request.Builder().url("https://www.baidu.com").build();
                try {
                    response = httpClient.newCall(request).execute();
                    if (response.isSuccessful()){
                        ResponseBody body = response.body();
                        body.byteStream();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if (response!=null){
                        response.close();
                    }
                    if (httpClient!=null){
                        httpClient.dispatcher().executorService().shutdown();
                        httpClient.connectionPool().evictAll();
                    }
                }

            }
        }.start();
    }
}
